package com.xpand.starter.canal.util;

import com.alibaba.fastjson2.JSONObject;
import com.alibaba.otter.canal.protocol.CanalEntry;

import java.util.List;

public class RowDataParser {
    private RowDataParser() {

    }

    public static <T> T parseBefore(CanalEntry.RowData rowData, Class<T> t) {
        List<CanalEntry.Column> beforeColumnsList = rowData.getBeforeColumnsList();
        JSONObject beforeData = new JSONObject();
        for (CanalEntry.Column column : beforeColumnsList) {
            beforeData.put(column.getName(), column.getValue());
        }
        return beforeData.toJavaObject(t);
    }

    public static <T> T parserAfter(CanalEntry.RowData rowData, Class<T> t) {
        List<CanalEntry.Column> afterColumnsList = rowData.getAfterColumnsList();
        JSONObject beforeData = new JSONObject();
        for (CanalEntry.Column column : afterColumnsList) {
            beforeData.put(column.getName(), column.getValue());
        }
        return beforeData.toJavaObject(t);
    }
}
